// pages/member/member.js
import {
  request
} from "../../request/index.js";
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {},
    package: {},
    userIdByResource: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.isshare == 1) {
      wx.navigateTo({
        url: '../guang/guang'
      })
    }
    const {
      userId
    } = options
    const {
      userIdByResource
    } = options
    var that = this


    request({
      url: "/UserInfo/getUserInfo",
      data: {
        userId: userId
      }
    }).then(res => {
      that.setData({
        user: res.data,
        userIdByResource: userIdByResource
      })
    })
    request({
      url: "/MembershipPackage/getAllMembershipPackage"
    }).then(res => {
      that.setData({
        package: res.data
      })
    })
  },

  buy(e) {
    // console.log(e.currentTarget.dataset.packagefee)
    // console.log(e.currentTarget.dataset.packagename)
    // console.log(e.currentTarget.dataset.packageterm)
    var that = this
    var userId = app.globalData.userId
    wx.login({
      success: function (res) {
        request({
          url: '/UserInfo/getCode',
          data: {
            code: res.code
          }
        }).then(r => {
          console.log(r)
          request({
            url: "/WxPay/prePay",
            data: {
              body: e.currentTarget.dataset.packagename,
              openid: r.data,
              userId: userId,
              money: e.currentTarget.dataset.packagefee,
              packageFee: e.currentTarget.dataset.packagefee,
              packageTerm: e.currentTarget.dataset.packageterm,
              packageName: e.currentTarget.dataset.packagename
            }
          }).then(res => {
            console.log(res)
            wx.requestPayment({
              nonceStr: res.data.payMap.nonceStr,
              package: res.data.payMap.package,
              paySign: res.data.payMap.paySign,
              timeStamp: res.data.payMap.timeStamp,
              signType: 'MD5',
              success(er) {
                if (!that.data.userIdByResource) {
                  request({
                    url: "/UserInfo/payMembershipPackage",
                    data: {
                      userId: userId,
                      payOrderId: res.data.payMap.payOrderId,
                      packageFee: e.currentTarget.dataset.packagefee,
                      packageTerm: e.currentTarget.dataset.packageterm,
                      packageName: e.currentTarget.dataset.packagename,
                    }
                  })
                } else {
                  request({
                    url: "/UserInfo/payMembershipPackage",
                    data: {
                      userId: userId,
                      userIdByResource: that.data.userIdByResource,
                      payOrderId: res.data.payMap.payOrderId,
                      packageFee: e.currentTarget.dataset.packagefee,
                      packageTerm: e.currentTarget.dataset.packageterm,
                      packageName: e.currentTarget.dataset.packagename,
                    }
                  })
                }

              },
              fail(s) {

              }
            })
          })
        })
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let pages = getCurrentPages(); //获取小程序页面栈
    //console.log(pages)
    let beforePage = pages[pages.length - 2]; //获取上个页面的实例对象
    beforePage.onLoad(beforePage.options);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/pages/member/member?isshare=1',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})